package cn.iocoder.yudao.module.cdk.controller.admin.cdk;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.cdk.controller.admin.cdk.vo.CdkPageReqVO;
import cn.iocoder.yudao.module.cdk.controller.admin.cdk.vo.CdkRespVO;
import cn.iocoder.yudao.module.cdk.controller.admin.cdk.vo.CdkSaveReqVO;
import cn.iocoder.yudao.module.cdk.dal.dataobject.cdk.CdkDO;
import cn.iocoder.yudao.module.cdk.service.cdk.CdkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - cdk")
@RestController
@RequestMapping("/cdk/cdk")
@Validated
public class CdkController {

    @Resource
    private CdkService Service;

    @PostMapping("/create")
    @Operation(summary = "创建cdk")
    @PreAuthorize("@ss.hasPermission('cdk::create')")
    public CommonResult<Integer> create(@Valid @RequestBody CdkSaveReqVO createReqVO) {
        return success(Service.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新cdk")
    @PreAuthorize("@ss.hasPermission('cdk::update')")
    public CommonResult<Boolean> update(@Valid @RequestBody CdkSaveReqVO updateReqVO) {
        Service.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除cdk")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cdk::delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Integer id) {
        Service.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得cdk")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cdk::query')")
    public CommonResult<CdkRespVO> get(@RequestParam("id") Integer id) {
        CdkDO cdk = Service.get(id);
        return success(BeanUtils.toBean(cdk, CdkRespVO.class));
    }


    @GetMapping("/page")
    @Operation(summary = "获得cdk分页")
    @PreAuthorize("@ss.hasPermission('cdk::query')")
    public CommonResult<PageResult<CdkRespVO>> getPage(@Valid CdkPageReqVO pageReqVO) {
        PageResult<CdkDO> pageResult = Service.getPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CdkRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出cdk Excel")
    @PreAuthorize("@ss.hasPermission('cdk::export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExcel(@Valid CdkPageReqVO pageReqVO,
                            HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CdkDO> list = Service.getPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "cdk.xls", "数据", CdkRespVO.class,
                BeanUtils.toBean(list, CdkRespVO.class));
    }

}